/*
	Copyright (C) 2009 jungil Han <jungil.Han@gmail.com>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mkAnimation.h"
#include <QTimer>

mkAnimation::mkAnimation( IN QWidget *a_pParent ) : QWidget( a_pParent )
{
	m_pSourceImage = NULL;

	m_pSourceImageRect = new QRect;
	m_pAnimationImageRect = new QRect;
	m_pMaxSize = new QSize;

	m_nMilliSec = 11;	/* Initial value is adjusted mkHotkeybutton */
	m_bIsIncrease = true;
	m_eType = eResizeEffect;

	m_pAnimationTimer = new QTimer( this );
	connect( m_pAnimationTimer, SIGNAL( timeout() ), this, SLOT( SlotAnimationEffect() ));
}

mkAnimation::~mkAnimation()
{
	MK_DELETE( m_pAnimationTimer );

	MK_DELETE( m_pSourceImageRect );
	MK_DELETE( m_pAnimationImageRect );
	MK_DELETE( m_pMaxSize );
}

void mkAnimation::SetMaximumAnimationSize( IN QSize *a_pMaxSize )
{
	if ( a_pMaxSize )
		memcpy( m_pMaxSize, a_pMaxSize, sizeof( QSize ));
}

bool mkAnimation::IsActivate()
{
	return m_pAnimationTimer->isActive();
}

QRect* mkAnimation::GetAnimationImageRect()
{
	return m_pAnimationImageRect;
}

void mkAnimation::StartAnimationEffect( IN QImage *a_pSourceImage, IN QRect *a_pSourceImageRect, eANIMATION_TYPE a_eType )
{
	if ( m_pAnimationTimer->isActive() )
		return ;

	if ( m_eType == eResizeEffect ) {
		if (m_pMaxSize->width() <= m_pSourceImageRect->width() || m_pMaxSize->height() <= m_pSourceImageRect->height() )
			return ;

		memcpy( m_pSourceImageRect, a_pSourceImageRect, sizeof( QRect ));
		memcpy( m_pAnimationImageRect, a_pSourceImageRect, sizeof( QRect ));
	}

	m_pAnimationTimer->start( m_nMilliSec );
}

void mkAnimation::SetAnimationTick( IN int a_nMilliSec )
{
	m_nMilliSec = a_nMilliSec;
}

void mkAnimation::SlotAnimationEffect()
{
	if ( m_eType == eResizeEffect )
		ResizeEffect();
}

void mkAnimation::ResizeEffect()
{
	if ( m_pAnimationImageRect->width() % 2 ) {
		if ( m_bIsIncrease ) {
			m_pAnimationImageRect->setX( m_pAnimationImageRect->x() - 1 );
			m_pAnimationImageRect->setY( m_pAnimationImageRect->y() - 1 );
		} else {
			m_pAnimationImageRect->setX( m_pAnimationImageRect->x() + 1 );
			m_pAnimationImageRect->setY( m_pAnimationImageRect->y() + 1 );
		}
	} else {
		if ( m_bIsIncrease ) {
			m_pAnimationImageRect->setWidth( m_pAnimationImageRect->width() + 1 );
			m_pAnimationImageRect->setHeight( m_pAnimationImageRect->height() + 1 );
		} else {
			m_pAnimationImageRect->setWidth( m_pAnimationImageRect->width() - 1 );
			m_pAnimationImageRect->setHeight( m_pAnimationImageRect->height() - 1 );
		}
	}

	/* Increase size upper limit(max size) and then decrease size */
	if( m_pAnimationImageRect->width() >= m_pMaxSize->width() || m_pAnimationImageRect->height() >= m_pMaxSize->height() ) {
		m_bIsIncrease = false;
	} else if ( m_pAnimationImageRect->width() <= m_pSourceImageRect->width() || m_pAnimationImageRect->height() <= m_pSourceImageRect->height() ) {
		m_bIsIncrease = true;
		m_pAnimationTimer->stop();
	}

	parentWidget()->update();
}
